package com.java110.mall.cart.storeOrderEngine;

import com.alibaba.fastjson.JSONArray;
import com.java110.dto.account.AccountDto;
import com.java110.dto.distributionMode.DistributionModeDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.acct.IAccountInnerServiceSMO;
import com.java110.intf.goods.IDistributionModeInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.po.account.AccountDetailPo;
import com.java110.po.storeOrder.StoreOrderPo;
import com.java110.utils.cache.MappingCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName StoreOrderDistribution
 * @Description TODO
 * @Author wuxw
 * @Date 2022/5/21 1:04
 * @Version 1.0
 * add by wuxw 2022/5/21
 **/
@Component
public class StoreOrderDistribution {

    @Autowired
    private IDistributionModeInnerServiceSMO distributionModeInnerServiceSMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;
    @Autowired
    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;

    public void doDistributionMode(StoreOrderPo storeOrderPo, JSONArray goodsList) {
        DistributionModeDto distributionModeDto = null;
        if (StoreOrderCartDto.CART_TYPE_PRODUCT.equals(storeOrderPo.getOrderType())) {
            distributionModeDto = getShopDistributionMode(goodsList);
            if (distributionModeDto != null) {
                //如果配送为金钱结算与商品合并计费,目前方式只能为固定费用方式，其他方式后续完善
                //if (DistributionModeDto.FEE_MODE_TYPE_M.equals(MappingCache.getValue(DistributionModeDto.DOMAIN_AMLL, DistributionModeDto.FEE_MODE_TYPE))) {
                if(DistributionModeDto.MODE_TYPE_STORE.equals(distributionModeDto.getModeType())){
                    BigDecimal fixedFee = new BigDecimal(Double.parseDouble(distributionModeDto.getFixedFee()));
                    double tempPayPrice = fixedFee.add(new BigDecimal(Double.parseDouble(storeOrderPo.getPayPrice()))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    storeOrderPo.setPayPrice(tempPayPrice + "");
                }
            }

        } else if (StoreOrderCartDto.CART_TYPE_SERV.equals(storeOrderPo.getOrderType())) {
            //如果为时间支付方式则直接扣平台时间账户款项，最终返回给font层为0
            if (DistributionModeDto.FEE_MODE_TYPE_T.equals(MappingCache.getValue(DistributionModeDto.DOMAIN_AMLL, DistributionModeDto.FEE_MODE_TYPE))) {
                myWithholdAccount(storeOrderPo.getPersonId(), storeOrderPo.getOrderId(), Double.valueOf(storeOrderPo.getPayPrice()));
                //如果为时间支付方式则直接扣平台时间账户款项，最终返回给font层为0
                storeOrderPo.setPayPrice("0");
            }
        }
    }

    private DistributionModeDto getShopDistributionMode(JSONArray goodsList) {
        if (goodsList == null || goodsList.size() < 1) {
            return null;
        }
        DistributionModeDto distributionModeDto = new DistributionModeDto();
        distributionModeDto.setShopId(goodsList.getJSONObject(0).getString("shopId"));
        List<DistributionModeDto> shopDistributionModeDtos = distributionModeInnerServiceSMOImpl.queryDistributionModes(distributionModeDto);
        if (shopDistributionModeDtos == null || shopDistributionModeDtos.size() < 1) {
            return null;
        }
        return shopDistributionModeDtos.get(0);
    }


    private void myWithholdAccount(String userId, String orderId, double amount) {
        UserDto userDto = new UserDto();
        userDto.setUserId(userId);
        List<UserDto> userDtoList = userV1InnerServiceSMOImpl.queryUsers(userDto);
        if (userDtoList == null || userDtoList.size() < 1) {
            throw new IllegalArgumentException("用户信息查询失败，请检查用户状态");
        }
        UserDto userTel = new UserDto();
        userTel.setTel(userDtoList.get(0).getTel());
        List<UserDto> userDtoListTel = userV1InnerServiceSMOImpl.queryUsers(userTel);
        if (userDtoListTel == null || userDtoListTel.size() < 1) {
            throw new IllegalArgumentException("通过用户手机查询失败，请检查用户状态");
        }
        List<String> temObjId = new ArrayList<>();
        for (UserDto user : userDtoListTel) {
            temObjId.add(user.getUserId());
        }
        AccountDto accountDto = new AccountDto();
        accountDto.setObjIds(temObjId.toArray(new String[temObjId.size()]));
        accountDto.setObjType(AccountDto.OBJ_TYPE_PERSON);
        accountDto.setAcctType(AccountDto.ACCT_TYPE_TIME);
        List<AccountDto> accountDtoList = accountInnerServiceSMOImpl.queryAccounts(accountDto);
        if (accountDtoList == null || accountDtoList.size() < 1) {
            throw new IllegalArgumentException("账户信息查询失败，请检查账户状态");
        }

        AccountDetailPo accountDetailPo = new AccountDetailPo();
        accountDetailPo.setAcctId(accountDtoList.get(0).getAcctId());
        accountDetailPo.setObjId(accountDtoList.get(0).getObjId());
        accountDetailPo.setObjType(accountDtoList.get(0).getAcctType());
        accountDetailPo.setOrderId(orderId);
        accountDetailPo.setAmount(amount + "");
        int flag = accountInnerServiceSMOImpl.withholdAccount(accountDetailPo);
        if (flag < 1) {
            throw new IllegalArgumentException("扣款失败");
        }
    }
}
